package com.colorgame.connection;

import java.util.ArrayList;
import java.util.Set;

import com.colorgame.main.Logger;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class SimpleBluetooth {
	private static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	private static BroadcastReceiver mReceiver = null;

	final private static int REQUEST_ENABLE_BT = 1;
	public static Activity activity = null;
	
	
//Bluetooth	
	public static boolean hasBluetooth() {
		return (mBluetoothAdapter != null);
	}		
	public static boolean isEnabled() {
		return mBluetoothAdapter.isEnabled();
	}

	public static void enable() {
		if (!mBluetoothAdapter.isEnabled()) {
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
	}
	public static void disable() {
		if (mBluetoothAdapter.isEnabled()) {
			mBluetoothAdapter.disable();
		}
	}
	
	public static void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_ENABLE_BT) {
			if (resultCode == activity.RESULT_OK) {
				mBluetoothAdapter.enable();
			} else
			if (resultCode == activity.RESULT_CANCELED) {
				Logger.log("Bluetooth denied");
			}
		}
		
	}

	
//Client
	public static ArrayList<BluetoothDevice> startDiscovery() {
		mBluetoothAdapter.startDiscovery();	//?
		
		ArrayList<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
		
		getPairedDevices(devices);
		discoverDevices(devices);
		
		return devices;
	}	
	public static void stopDiscovery() {
		activity.unregisterReceiver(mReceiver);
		
		mBluetoothAdapter.cancelDiscovery();	//?
	}
	
	private static void getPairedDevices(ArrayList<BluetoothDevice> list) {
		Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
		
		if (pairedDevices.size() > 0) {
		    for (BluetoothDevice device : pairedDevices) {
		    	if (!list.contains(device)) {
			        list.add(device);
				}
		    }
		}
		
		return;
	}
	private static void discoverDevices(final ArrayList<BluetoothDevice> list) {
		mReceiver = new BroadcastReceiver() {
		    public void onReceive(Context context, Intent intent) {
		        String action = intent.getAction();
		        // When discovery finds a device
		        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
		            // Get the BluetoothDevice object from the Intent
		            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
		            // Add the device to an array
			    	if (!list.contains(device)) {
				        list.add(device);
					}
		        }
		    }
		};

		// Register the BroadcastReceiver
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		activity.registerReceiver(mReceiver, filter);
	}

//Host	
	public static void startHost() {
		Intent discoverableIntent = new
				Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
				discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
				activity.startActivity(discoverableIntent);
	}
	public static void stopHost() {
		// TODO Auto-generated method stub

	}
}
